package fr.jee.web.bean.administration.news;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;

import fr.jee.ejb.facade.NewsFacade;
import fr.jee.ejb.model.News;
import fr.jee.ejb.outils.enumeration.TypeModificationEnum;
import fr.jee.web.outils.JsfUtils;

@Named
@ViewScoped
public class AdministrationNewsBean implements Serializable {

	@Inject
	private NewsFacade newsFacade;
	private List<News> listeNews = new ArrayList();
	private News news;
	private TypeModificationEnum typeModification;

	public AdministrationNewsBean() {
	}

	@PostConstruct
	private void initialisation() {
		rechercher();
	}

	private void rechercher() {
		listeNews = newsFacade.findAllNewsOrderByDateCreation();
	}

	public void initNouvelleNews() {
		news = new News();
		typeModification = TypeModificationEnum.creation;
		JsfUtils.afficherDialog("_dialogModificationNews");
	}

	public void selectionNews() {
		typeModification = TypeModificationEnum.modification;
		JsfUtils.afficherDialog("_dialogModificationNews");
	}

	public void valider() {
		news = newsFacade.editerNews(news, typeModification);
		JsfUtils.ajoutMessageInfo("La news a bien été éditée");
		rechercher();
		JsfUtils.cacherDialog("_dialogModificationNews");
	}

	public List<News> getListeNews() {
		return listeNews;
	}

	public void setListeNews(List<News> listeNews) {
		this.listeNews = listeNews;
	}

	public News getNews() {
		return news;
	}

	public void setNews(News news) {
		this.news = news;
	}

	public TypeModificationEnum getTypeModification() {
		return typeModification;
	}

	public void setTypeModification(TypeModificationEnum typeModification) {
		this.typeModification = typeModification;
	}

}
